#ifndef __SBUF_H__
#define __SBUF_H__

#include <semaphore.h>

typedef struct {
    int     *buf;   /* buffer array */
    int     n;      /* maximum number of slots */
    int     front;  /* buf[(front+1)%n] is first item */
    int     rear;   /* buf[rear%n] is last item */
    sem_t   mutex;  /* protects access to buf */
    sem_t   slots;  /* counts available slots */
    sem_t   items;  /* counts available items */
} sbuf_t;

void P(sem_t *s);
void V(sem_t *s);

void sbuf_init(sbuf_t *sp, int n);
void sbuf_insert(sbuf_t *sp, int item);
int sbuf_remove(sbuf_t *sp);


#endif
